package com.hxzy.controller.api.system;

import cn.hutool.crypto.digest.BCrypt;
import com.hxzy.common.vo.CodeEnum;
import com.hxzy.common.vo.R;
import com.hxzy.dto.system.SysPostDTO;
import com.hxzy.entity.system.SysPost;
import com.hxzy.service.system.SysPostService;
import com.hxzy.vo.system.HxzyUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = "岗位信息")
@RestController
@RequestMapping(value = "/api")
public class SysPostController {
    @Autowired
    private SysPostService sysPostService;

    @ApiOperation(value = "岗位分页信息")
    @PostMapping(value = "/syspost/list")
    public R serch(@RequestBody SysPostDTO sysPostDTO){
        return R.success(sysPostService.serch(sysPostDTO));
    }

    @ApiModelProperty(value = "新增数据")
    @PostMapping(value = "/syspost/add")
    public R add(@RequestBody @Valid SysPost sysPost){
        HxzyUser hxzyUser= (HxzyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        sysPost.setCreateBy(hxzyUser.getSysUser().getName());
        sysPost.setCreateTime(new Date());
        boolean save = sysPostService.save(sysPost);
        return save? R.success():R.failed();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/syspost/delete")
    public R delete(String ids){
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        boolean b = sysPostService.removeByIds(strings);
        return b?R.success():R.failed();
    }

    @ApiOperation(value = "修改")
    @PostMapping(value = "/syspost/update")
    public R update(@RequestBody SysPost sysPost){
        HxzyUser hxzyUser= (HxzyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysPost db = sysPostService.getById(sysPost.getId());
        if (db!=null){
            db.setPostName(sysPost.getPostName());
            db.setStatus(sysPost.getStatus());
            db.setPostCode(sysPost.getPostCode());
            db.setPostSort(sysPost.getPostSort());
            db.setRemark(sysPost.getRemark());
            db.setUpdateBy(hxzyUser.getSysUser().getName());
            db.setUpdateTime(new Date());
            boolean b = this.sysPostService.updateById(db);
            return b?R.success():R.failed();
        }else {
            return R.failed(CodeEnum.DATE_CATEGORY_FAILED);
        }

    }

    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParam(name = "id",value = "查询数据")
    @GetMapping(value = "/syspost/findById")
    public R findById( Long id){
        System.out.println(id);
        SysPost sysPost = this.sysPostService.findById(id);
        System.out.println("==========>"+sysPost);
        return R.success(sysPost);
    }
}
